package com.smxy.daydayup.util.osa.impl;

import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import com.smxy.daydayup.util.TenantContext;
import com.smxy.daydayup.util.osa.OSAProvider;
import com.smxy.daydayup.util.osa.ObjectStorageInfo;
import com.smxy.daydayup.util.osa.mapper.ObjectStorageInfoMapper;

import java.time.LocalDateTime;

/**
 * @Author: yusy
 * @Date: 2022/8/31
 */
public abstract class AbstractCloudStorageOSAProvider implements OSAProvider {
    protected final String bucket;
    protected final String group;

    protected AbstractCloudStorageOSAProvider(String bucket, String group) {
        this.bucket = bucket;
        this.group = group;
    }
    protected String getBucket(CustomeInfo customeInfo) {
        return this.bucket == null || this.bucket.isEmpty()? customeInfo.bucket : this.bucket;
    }
    protected String genFullPath(CustomeInfo customeInfo) {
        return (group + "/" + customeInfo.path + "/" + NanoIdUtils.randomNanoId()).replaceAll("//", "/");
    }

    protected void save(ObjectStorageInfoMapper mapper, ObjectStorageInfo data) {
        final TenantContext.Tenant tenant = TenantContext.getTenant();
        data.setCreatedAt(LocalDateTime.now());
        data.setModifiedAt(LocalDateTime.now());
        data.setCreatedId(tenant.getId());
        data.setCreatedName(tenant.getName());
        data.setMarkedForDel(false);
        data.setModifiedId(tenant.getId());
        data.setModifiedName(tenant.getName());

        mapper.insert(data);
    }
}
